﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FWMS
{
    public partial class Form_ruku_dateselect : Form
    {
        public Form_ruku_dateselect()
        {
            InitializeComponent();
        }
        //初始化
        private void Form_ruku_dateselect_Load(object sender, EventArgs e)
        {
            //开始日期
            string startYear = this.startdate.Value.Year.ToString();
            string startMonth = this.startdate.Value.Month.ToString();
            this.startdate.Text = startYear + "年" + startMonth + "月" + "1日";
            //结束日期
            this.enddate.Value = System.DateTime.Now;
            COMMAND.CommandBean.rukuyuancailiaoresult = null;
            COMMAND.CommandBean.yanshoudate = null;
            COMMAND.CommandBean.no = null;
            if ("3".Equals(COMMAND.CommandBean.cailiaostyle.ToString()))
            {
                this.yanshoudate.HeaderText = "赏味日期";
                this.daohuodate.HeaderText = "生产日期";
                this.tb_all_tijiao.Enabled = true;
            }
            else
            {
                this.yanshoudate.HeaderText = "验收日期";
                this.daohuodate.HeaderText = "到货日期";
                this.tb_all_tijiao.Enabled = false;
            }


        }
        //查询
        private void select_Click(object sender, EventArgs e)
        {
            if (int.Parse(this.startdate.Value.ToString("yyyyMMdd")) > int.Parse(this.enddate.Value.ToString("yyyyMMdd")))
            {
                MessageBox.Show("开始日期不能在终止日期之后", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.datacom.Rows.Clear();
            this.exprot.UseColumnTextForButtonValue = true;
            Service.Rukuguanli_dateselect dateselecdt = new FWMS.Service.Rukuguanli_dateselect();
            object[,] result = null;
            if ("3".Equals(COMMAND.CommandBean.cailiaostyle.ToString()))
            {
                result = dateselecdt.rukuguanli_chanpinServiceSelect(this.startdate.Value.ToString("yyyyMMdd"), this.enddate.Value.ToString("yyyyMMdd"),
                    COMMAND.CommandBean.cailiaostyle.ToString(), "", "");
            }
            else
            {
                result = dateselecdt.rukuguanli_ServiceSelect(this.startdate.Value.ToString("yyyyMMdd"), this.enddate.Value.ToString("yyyyMMdd"),
                    COMMAND.CommandBean.cailiaostyle.ToString(), "", "");

            }
            if (result == null)
            {
                MessageBox.Show("所选时间段内没有出库记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                if ("3".Equals(COMMAND.CommandBean.cailiaostyle.ToString()))
                {
                    for (int j = 0; j < result.Length / 4; j++)
                    {//ruku_yanshou,yuanliaoruku_pinming,ruku_guige,ruku_daohuo
                        this.datacom.Rows.Add();
                        this.datacom.Rows[j].Cells[1].Value = result[j, 1];//品名
                        this.datacom.Rows[j].Cells[2].Value = result[j, 2];//规格
                        this.datacom.Rows[j].Cells[3].Value = result[j, 0];//验收日期
                        this.datacom.Rows[j].Cells[4].Value = result[j, 3];//到货日期
                        this.exprot.Text = "查看详细";
                        COMMAND.CommandBean.rukuyuancailiaoresult = null;
                    }
                }
                else
                {
                    for (int j = 0; j < result.Length / 6; j++)
                    {
                        this.datacom.Rows.Add();
                        this.datacom.Rows[j].Cells[0].Value = result[j, 0];//NO号
                        this.datacom.Rows[j].Cells[1].Value = result[j, 1];//到货日期
                        this.datacom.Rows[j].Cells[2].Value = result[j, 2];//验收日期
                        this.datacom.Rows[j].Cells[3].Value = result[j, 3];//到货日期
                        this.datacom.Rows[j].Cells[4].Value = result[j, 4];//验收日期
                        this.datacom.Rows[j].Cells[5].Value = result[j, 5];//发货单位
                        this.exprot.Text = "查看详细";
                    }
                }
            }

        }
        //提交
        private void datacom_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (this.datacom.SelectedCells.Count == 0)
            {
                return;
            }
            if (this.datacom.SelectedCells[0].ColumnIndex == 6)
            {
                if ("3".Equals(COMMAND.CommandBean.cailiaostyle.ToString()))
                {
                    COMMAND.CommandBean.daohuodate = this.datacom.Rows[this.datacom.SelectedCells[0].RowIndex].Cells[4].Value.ToString();
                    COMMAND.CommandBean.yanshoudate = this.datacom.Rows[this.datacom.SelectedCells[0].RowIndex].Cells[3].Value.ToString();
                    this.Close();
                }
                else
                {
                    COMMAND.CommandBean.no = this.datacom.Rows[this.datacom.SelectedCells[0].RowIndex].Cells[0].Value.ToString();
                    this.Close();
                }

            }
        }
        //时间段数据全部提交
        private void tb_all_tijiao_Click(object sender, EventArgs e)
        {
            object[,] result = null;
            Service.Rukuguanli_dateselect dateselecdt = new FWMS.Service.Rukuguanli_dateselect();
            result = dateselecdt.rukuguanli_chanpinjijiServiceSelect(this.startdate.Value.ToString("yyyyMMdd"), this.enddate.Value.ToString("yyyyMMdd"));
            if (result == null)
            {
                MessageBox.Show("所选时间段内没有出库记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            COMMAND.CommandBean.yanshoudate = "0000";
            COMMAND.CommandBean.daohuodate = "0000";
            COMMAND.CommandBean.rukuyuancailiaoresult = result;
            this.Close();
        }
        //第二查询执行
        private void selectallcondition_Click(object sender, EventArgs e)
        {

            string pinming = this.pinming.Text.ToString().Trim();
            string guige = this.guige.Text.ToString().Trim();
            if (int.Parse(this.startdate.Value.ToString("yyyyMMdd")) > int.Parse(this.enddate.Value.ToString("yyyyMMdd")))
            {
                MessageBox.Show("开始日期不能在终止日期之后", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if ("".Equals(pinming.ToString().Trim()))
            {
                MessageBox.Show("品名不能为空", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.selectconditionall.Rows.Clear();
            Service.Rukuguanli_dateselect dateselecdt = new FWMS.Service.Rukuguanli_dateselect();
            object[,] result = null;
            if ("3".Equals(COMMAND.CommandBean.cailiaostyle.ToString()))
            {
                result = dateselecdt.rukuguanli_chanpinServiceSelect(this.startdate.Value.ToString("yyyyMMdd"), this.enddate.Value.ToString("yyyyMMdd"),
                    COMMAND.CommandBean.cailiaostyle.ToString(), pinming, guige);
            }
            else
            {
                result = dateselecdt.rukuguanli_ServiceSelect(this.startdate.Value.ToString("yyyyMMdd"), this.enddate.Value.ToString("yyyyMMdd"),
                    COMMAND.CommandBean.cailiaostyle.ToString(), pinming, guige);

            }

            if (result == null)
            {
                MessageBox.Show("所选时间段内没有出库记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                for (int j = 0; j < result.Length / 6; j++)
                {
                    this.selectconditionall.Rows.Add();
                    this.selectconditionall.Rows[j].Cells[0].Value = result[j, 0];//NO号
                    this.selectconditionall.Rows[j].Cells[1].Value = result[j, 1];//品名
                    this.selectconditionall.Rows[j].Cells[2].Value = result[j, 2];//规格
                    this.selectconditionall.Rows[j].Cells[3].Value = result[j, 3];//到货日期
                    this.selectconditionall.Rows[j].Cells[4].Value = result[j, 4];//验收日期
                    this.selectconditionall.Rows[j].Cells[5].Value = result[j, 5];//发货单位
                }
            }

        }
        //自动求和
        private void button2_Click(object sender, EventArgs e)
        {
            float sum = 0;
            for (int i = 0; i < this.selectconditionall.GetCellCount(DataGridViewElementStates.Selected); i++)
            {
                if (COMMAND.Command.IsFloat(this.selectconditionall.SelectedCells[i].Value.ToString()))
                {
                    sum = sum +  float.Parse( this.selectconditionall.SelectedCells[i].Value.ToString());
                }
                else
                {
                    MessageBox.Show("选中的数据存在非数值类型！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                
            }
            MessageBox.Show("当前选中数据之合是：" + Math.Round(sum,3), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
